<?php
/*
*	created by cbq in 2017.02.08
*	学生相关模型
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Student_m extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->table='student';
    }

    /**
     * 获取学生列表
     * @param array $params
     * @return array
     */
    public function getStudentList($params=array())
    {
        $sql = '';
        if(!empty($params['status']))
        {
            $sql .= ' AND a.`status` = '.$params['status'];
        }
        if (!empty($params['stuid'])) {
            $sql .= ' AND a.`stuid` LIKE \'%' . $params['stuid'] . '%\'';
        }
        if (!empty($params['start_time']))
        {
            $sql .= ' AND a.`year` >= '.$params['start_time'];
        }
        if (!empty($params['end_time']))
        {
            $sql .= ' AND a.`year` <= '.$params['end_time'];
        }
        $order = ' ORDER BY a.`id` DESC';
        $select_sql = ' FROM `akr_student` as a WHERE 1'. $sql.$order;
        $current_sql = 'SELECT count(a.`id`) as count'. $select_sql;
        $count = $this->db->query($current_sql);
        $count = $count->result_array()[0]['count'];
        $count = intval($count);
        $data['count'] = $count;
        if($data['count'] < 1)
        {
            $data['list'] = array();
            $data['page'] = null;
            return $data;
        }
        $limit = '';
        $page_size = 0;
        if(!empty($params['page_size']) && intval($params['page_size'])>0)
        {
            $page_size = intval($params['page_size']);
        }
        else
        {
            $page_size = getConfig('pagesize');
        }
        if(!empty($params['page']) && intval($params['page'])>0)
        {
            $page = intval($params['page']);
            $first = ($page-1)*$page_size;
            if($first < $count){
                $limit = ' LIMIT '.$first.','.$page_size;
            }else{
                $data['list'] = array();
                $data['page'] = null;
                return $data;
            }
        }
        $current_sql = 'SELECT a.*'. $select_sql.$limit;
        $list = $this->db->query($current_sql);
        $list = $list->result_array();
        $sexs = array(0=>'女', 1=>'男');
        foreach ($list as &$value)
        {
            $value['sex'] = $sexs[$value['sex']];
            $value['year'] = date('Y', $value['year']);
        }
        $data['list'] = $list;
        $data['page'] = $this->student_m->pageajax_show($page_size,$count);
        return $data;
    }
    public function ajaxall($where=array(),$field="*",$order='id asc'){
        $pagesize=getConfig('pagesize');
        $this->com_where($where);
        $this->db->select('count(id) as counts');
        $query=$this->db->get($this->table);
        $counts=$query->row_array();

        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$pagesize * ($page - 1);
        $this->com_where($where);
        $this->db->select($field);
        $this->db->order_by($order);
        $this->db->limit($pagesize,$startRow);
        $query2=$this->db->get($this->table);
        $data['list']=$query2->result_array();
        $data['pageshow']=$this->pageajax_show($pagesize,$counts['counts']);
        return $data;
    }
    /**
     * 查询单条学生信息
     * @param array $where
     * @param string $field
     * @return mixed
     */
    public function sel($where=array(),$field="*"){
    	  $this->com_where($where);
    	  $this->db->select($field);
        $query=$this->db->get($this->table);
     	  $data=$query->row_array();
        return $data;
    }
    /**
     * 添加学生
     * @param $data
     * @return bool
     */
    public function add($data){
        $where['stuid'] = array('=', $data['stuid']);
        $stuids = $this->sel($where);
        if($stuids['stuid'])
        {
            return false;
        }
        unset($where['stuid']);
        $where['mobile'] = array('=', $data['mobile']);
        $mobiles = $this->sel($where);
        if($mobiles && in_array($data['mobile'], $mobiles))
        {
            return false;
        }
        $res = $this->db->insert($this->table,$data);
        if($this->db->insert_id()===false){
          return false;
        }else{
            $addid = $this->db->insert_id();
        }
        return $addid;
    }
    //编辑学生
    public function edit($where,$data){
        $this->com_where($where);
        $this->db->update($this->table,$data);
        return $this->db->affected_rows();
    }
    //删除学生
    public function del($where){
        $this->com_where($where);
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }
    //更新学生状态
    public function update($id, $status)
    {
        print_r($id, $status);die;
        $this->db->set('status', $status);
        $this->db->where('id', $id);
        $this->db->update('akr_student');
        return $this->db->affected_rows();
    }
    //批量获取id
    public function getIds($params=array())
    {
        $sql = '';
        if($params && isset($params['status']))
        {
            $sql .= ' AND a.`status` = '.$params['status'];
        }
        if($params && isset($params['start_time']) && isset($params['end_time']))
        {
            $sql .= ' AND a.`year` BETWEEN '.$params['start_time'].' AND '.$params['end_time'];
        }
        if($params && isset($params['stuid']))
        {
            $sql .= ' AND a.`stuid` LIKE \'%'.$params['stuid'].'%\'';
        }
        if($params && isset($params['page_size']))
        {
            $page_size = $params['page_size'];
        }
        else
        {
            $page_size = getConfig('pagesize');
        }
        $select_sql = ' FROM `akr_student` as a WHERE 1'. $sql;
        $current_sql = 'SELECT count(a.`id`) as count'. $select_sql;
        $count = $this->db->query($current_sql);
        $count = $count->result_array()[0]['count'];
        $count = intval($count);
        $ids = array();
        $limit = '';
        if($params && isset($params['page']))
        {
            $first = ($params['page']-1)*$page_size;
            if($first < $count){
                $limit = ' LIMIT '.$first.','.$page_size;
            }else{
                return $ids;
            }
        }
        $current_sql = 'SELECT a.`id`'. $select_sql.$limit;
        $list = $this->db->query($current_sql);
        $list = $list->result_array();
        foreach ($list as $v)
        {
            $ids[] = $v['id'];
        }
        return $ids;
    }
    //查询所有学生
    public function selall($where=array(),$field='*'){
        $this->com_where($where);
        $this->db->select($field);
        $query=$this->db->get($this->table);
        $data=$query->result_array();
        return $data;
    }
}
?>